package com.bw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.resp.ResponseResult;
import com.bw.entity.TbResource;
import com.bw.entity.TbUser;
import com.bw.service.ITbUserService;
import com.bw.utils.auth.JwtUtils;
import com.bw.utils.auth.RsaUtils;
import com.bw.utils.auth.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PrivateKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = "角色管理")
public class TbUserController {

    @Autowired
    private ITbUserService userService;

    /**
     * TODO:角色登录功能
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("login")
    @ApiOperation(value = "角色登录", notes = "角色登录")
    @ApiImplicitParams(value = {
              @ApiImplicitParam(name = "user", value = "user", dataType = "TbUser", required = true)
    })
    public ResponseResult login(@RequestBody TbUser user){
        ResponseResult result  = ResponseResult.SUCCESS();
        try{
            //条件构造器
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("loginname",user.getLoginname());
            queryWrapper.eq("password",user.getPassword());

            //根据用户名和密码去查询
            TbUser user1 = userService.getOne(queryWrapper);
            //判断user1是否存在
            if(user1!=null){
                //获取私钥
                PrivateKey privateKey = RsaUtils.getPrivateKey("D:\\workSpace\\jwt\\rsa\\rsa.pri");
                //获取用户信息
                UserInfo userInfo = new UserInfo();
                userInfo.setId(user1.getId());
                userInfo.setUsername(user1.getUsername());
                //生成token 三个参数
                String token = JwtUtils.generateToken(userInfo,privateKey,30);
                Map map = new HashMap();
                map.put("user",user1);
                map.put("token",token);
                result.setResult(map);
                result.setMessage("登录成功");
            }else{
                result.setSuccess(false);
                result.setMessage("用户名或密码错误");
            }
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("登录异常"+e.toString());
        }
        return result;

    }

    /**
     * TODO:查看用户列表
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("list")
    @ApiOperation(value = "查看用户列表", notes = "查看用户列表")
    public ResponseResult list(){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            List<TbUser> list = userService.list();
            result.setResult(list);
            result.setSuccess(true);
        }catch (Exception e){
            result.setSuccess(false);
        }
        return result;
    }

}
